package com.cater.project.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cater.annotation.SystemControllerLog;
import com.cater.common.PageResult;
import com.cater.project.entity.DemandCommunicate;
import com.cater.project.entity.DemandOfferRecord;
import com.cater.project.service.DemandCommunicateServiceImpl;
import com.cater.project.service.DemandOfferRecordServiceImpl;
import com.cater.query.CommonQuery;
import com.cater.talents.entity.TalentsRecommend;
import com.cater.talents.service.TalentsRecommendServiceImpl;

/**
 * 描述：offer记录控制层
 * 
 * @author yong
 * @date 2019/04/02
 */
@Controller
@RequestMapping("/demandOfferRecord")
public class DemandOfferRecordController {

	@Autowired
	private DemandOfferRecordServiceImpl service;
	@Autowired
	private DemandCommunicateServiceImpl communicateService;
	@Autowired
	private TalentsRecommendServiceImpl recommendService;

	/**
	 * @offer记录管理列表视图
	 * @return
	 */
	@RequestMapping("/list")
	public String list() {
		return "project/demandOfferRecord/list";
	}

	/**
	 * @offer记录管理数据分页
	 * @param query
	 * @param request
	 * @return
	 */
	@RequestMapping("listData")
	@ResponseBody
	@SystemControllerLog(description = "查询offer记录分页数据")
	public PageResult<DemandOfferRecord> listData(CommonQuery query, HttpServletRequest request) {
		PageResult<DemandOfferRecord> result = service.findPageResult(query);
		return result;
	}

	/**
	 * 添加视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "addView")
	public String addView(CommonQuery query, Model model) {
		String communicateId = query.getCommunicateId();
		if (StringUtils.isNotEmpty(communicateId)) {
			List<DemandOfferRecord> records = service.getListBy(query);
			if(records.size()>0) {
				DemandOfferRecord record = records.get(0);
				if(record!=null) {
					model.addAttribute("record", record);
				}
			}
			model.addAttribute("communicateId", communicateId);
		}
		String recommendId = query.getRecommendId();
		if (StringUtils.isNotEmpty(recommendId)) {
			TalentsRecommend recommend = recommendService.getById(recommendId);
			if(recommend!=null) {
				model.addAttribute("recommend", recommend);
			}
		}
		return "project/demandOfferRecord/add";
	}

	/**
	 * 添加操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "add")
	@ResponseBody
	@SystemControllerLog(description = "添加offer记录")
	public JSONObject add(DemandOfferRecord t, CommonQuery query) {
		JSONObject json = new JSONObject();
		String communicateId = query.getCommunicateId();
		if (StringUtils.isNotEmpty(communicateId)) {
			DemandCommunicate communicate = communicateService.getById(communicateId);
			if (communicate != null) {
				t.setTrueName(communicate.getTrueName());
			}
		}
		String id = service.saves(t);
		if (StringUtils.isNotBlank(id)) {
			json.put("code", 200);
			json.put("message", "success");
		} else {
			json.put("code", 999);
			json.put("message", "fail");
		}
		return json;
	}

	/**
	 * 修改视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "editView")
	public String editView(String id, Model model, CommonQuery query) {
		DemandOfferRecord t = service.getById(id);
		model.addAttribute("demandOfferRecord", t);
		return "project/demandOfferRecord/update";
	}

	/**
	 * 修改操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "update")
	@ResponseBody
	@SystemControllerLog(description = "修改offer记录")
	public JSONObject update(DemandOfferRecord t, CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			service.update(t);
			json.put("code", 200);
			json.put("message", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "修改失败");
		}
		return json;
	}

	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("demandOfferRecord:delete")
	@SystemControllerLog(description = "删除offer记录")
	public JSONObject delete(String id) {
		JSONObject json = new JSONObject();
		try {
			service.delete(id);
			json.put("code", 200);
			json.put("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "删除失败");
		}
		return json;
	}

	/**
	 * 查看视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "detailView")
	public String detailView(String id, Model model, CommonQuery query) {
		DemandOfferRecord t = service.getById(id);
		model.addAttribute("demandOfferRecord", t);
		return "project/demandOfferRecord/detail";
	}

}